using System;
using System.Collections.Generic;
using System.Text;
using MacomberMap.Common.Components;
using System.Windows.Forms;
using System.ComponentModel;
using MacomberMap.Common.Types;
using MacomberMap.Common.Convertors;
using MacomberMap.Common.Internals;

namespace MacomberMap.Loader.User_Interfaces.Network_Map.Editor
{
    /// <summary>
    /// This class holds the editor front-end for an element
    /// </summary>
    public class MM_Editor_Element
    {
        #region Variable declarations
        /// <summary>The MM Element upon which this element is based</summary>
        public MM_Element BaseElement;

        /// <summary>The list view item associated with this element</summary>
        internal ListViewItem AssociatedItem;
        #endregion
        
        #region Variable retrieval
        /// <summary>
        /// The KV Level of the element
        /// </summary>
        [Category("Display"), Description("The KV Level of the element"), TypeConverter(typeof(MM_KVLevel_Locator))]
        public MM_KVLevel KVLevel
        {
            get { return BaseElement.KVLevel; }
            set { BaseElement.KVLevel = value; }
        }

        /// <summary>
        /// The CIM Identifier (TEID) of the element
        /// </summary>
        [Category("Declaration"), Description("The CIM Identifier (TEID) of the element")]
        public UInt64 TEID
        {
            get { return BaseElement.TEID; }
            set { BaseElement.TEID = value; }
        }

        /// <summary>
        /// The name of the element
        /// </summary>
        [Category("Display"), Description("The name of the element")]
        public string Name
        {
            get { return BaseElement.Name; }
            set { BaseElement.Name = value; }
        }
        #endregion      
  
        #region ListView handling
        /// <summary>
        /// Add this component to a list view
        /// </summary>
        /// <param name="ParentView">The list view to receive the new item</param>
        public void AddComponent(ListView ParentView)
        {
            //Start by building our associated element
            if (AssociatedItem == null)
                (AssociatedItem = ParentView.Items.Add(BaseElement.Name)).SubItems.Add("");
            else
                AssociatedItem.SubItems[0].Text = BaseElement.Name;
            
            //Now, assign our tag
            AssociatedItem.Tag = this;

            //Based on our element type, assign the other components
            if (this.BaseElement is MM_Boundary)
            {
                MM_Boundary Bound = (MM_Boundary)this.BaseElement;
                AssociatedItem.SubItems[1].Text = Bound.Website;
                AssociatedItem.Group = ParentView.Groups["Counties"];                
            }
            else if (this.BaseElement is MM_Substation)
            {
                MM_Substation Sub = (MM_Substation)this.BaseElement;
                AssociatedItem.SubItems[1].Text = Sub.LongName;
                if (MM_Repository.TEIDs.ContainsKey(Sub.TEID))
                    AssociatedItem.Group = ParentView.Groups["Substations"];
                else
                    AssociatedItem.Group = ParentView.Groups["Mismatched Substations"];
            }
            else if (this.BaseElement is MM_Line)
            {
                MM_Line Line = (MM_Line)this.BaseElement;
                AssociatedItem.SubItems[1].Text = Line.LongName;
                if (MM_Repository.TEIDs.ContainsKey(Line.TEID))
                    AssociatedItem.Group = ParentView.Groups["Lines"];
                else
                    AssociatedItem.Group = ParentView.Groups["Mismatched Lines"];
            }
        }
        #endregion

        #region Initialization
        /// <summary>
        /// Initialize a new editor element
        /// </summary>
        /// <param name="BaseElement">The element upon which the editor element is based</param>
        public MM_Editor_Element(MM_Element BaseElement)
        {
            this.BaseElement = BaseElement;
        }
        #endregion
    }
}
